/**
 * @author Jun
 * on Apr 27, 2012, 2012
 */
package com.fastwire.carriere.rad.snmpadapter;

import org.snmp4j.CommunityTarget;
import org.snmp4j.UserTarget;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TcpAddress;
import org.snmp4j.smi.UdpAddress;

public class SnmpUtil
{
    public static final int DEFAULT_VERSION = SnmpRadConstants.SNMP_Version;

    public static final String DEFAULT_PROTOCOL = SnmpRadConstants.SNMP_PROTOCOL;

    public static final int DEFAULT_PORT = SnmpRadConstants.SNMP_Port;

    public static long DEFAULT_TIMEOUT = 3 * 1000L;

    public static int DEFAULT_RETRY = 3;


    public static CommunityTarget createCommunityTarget(Address targetAddress, String community)
    {
        CommunityTarget target = new CommunityTarget();
        target.setCommunity(new OctetString(community));
        target.setAddress(targetAddress);
        
        target.setVersion(DEFAULT_VERSION);
        target.setTimeout(DEFAULT_TIMEOUT);  // milliseconds
        target.setRetries(DEFAULT_RETRY);
        
        return target;
    }

    
    /**
     * communityTarget
     * @param targetAddress
     * @param community
     * @param version
     * @return CommunityTarget
     */
    public static CommunityTarget createCommunityTarget(Address targetAddress, String community,
            int version)
    {
        CommunityTarget target = new CommunityTarget();
        target.setCommunity(new OctetString(community));
        target.setAddress(targetAddress);
        target.setVersion(version);
        target.setTimeout(DEFAULT_TIMEOUT);  // milliseconds
        target.setRetries(DEFAULT_RETRY);
        
        return target;
    }

    /**
     * communityTarget
     * @param targetAddress
     * @param community
     * @param version
     * @param timeOut
     * @param retry
     * @return CommunityTarget
     */
    public static CommunityTarget createCommunityTarget(Address targetAddress, String community,
            int version, long timeOut, int retry)
    {
        CommunityTarget target = new CommunityTarget();
        target.setCommunity(new OctetString(community));
        target.setAddress(targetAddress);
        target.setVersion(version);
        target.setTimeout(timeOut); // milliseconds
        target.setRetries(retry);
        
        return target;
    }

    /**
     * communityTarget
     * @param address
     * @param community
     * @param version
     * @param timeOut
     * @param retry
     * @return CommunityTarget
     */
    public static CommunityTarget createCommunityTarget(String address, String community,
            int version, long timeOut, int retry)
    {
        Address targetAddress = GenericAddress.parse(address);
        return createCommunityTarget(targetAddress, community, version, timeOut, retry);
    }

    /**
     * snmp Address
     * @param protocol
     * @param ip
     * @param port
     * @return Address
     */
    public static Address createAddress(String protocol, String ip, int port)
    {
        String address = protocol + ":" + ip + "/" + port;
        return GenericAddress.parse(address);
    }

    /**
     * snmp udp Address
     * @param ip
     * @param port
     * @return Address
     */
    public static Address createUdpAddress(String ip, int port)
    {
        String address = ip + "/" + port;
        return new UdpAddress(address);
    }

    /**
     * snmp tcp Address
     * @param ip
     * @param port
     * @return Address
     */
    public static TcpAddress createTcpAddress(String ip, int port)
    {
        String address = ip + "/" + port;
        return new TcpAddress(address);
    }

    /**
     *  UserTarget
     * @param targetAddress
     * @param version
     * @param timeOut
     * @param level
     * @param securityName
     * @return UserTarget
     */
    public static UserTarget createUserTarget(Address targetAddress, int version, long timeOut,
            int level, String securityName)
    {
        UserTarget target = new UserTarget();
        target.setAddress(targetAddress);
        target.setRetries(1);
        target.setTimeout(timeOut); // milliseconds
        target.setVersion(version);
        target.setSecurityLevel(level);
        target.setSecurityName(new OctetString(securityName));
        
        return target;
    }

    /**
     *  UserTarget
     * @param address
     * @param version
     * @param timeOut
     * @param level
     * @param securityName
     * @return UserTarget
     */
    public static UserTarget createUserTarget(String address, int version, long timeOut, int level,
            String securityName)
    {
        Address targetAddress = GenericAddress.parse(address);
        
        return createUserTarget(targetAddress, version, timeOut, level, securityName);
    }

}
